package com.lanou.controller;

import com.lanou.bean.Account;
import com.lanou.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * controller
 */
@Controller
public class AccountController {

    @Autowired
    private AccountService accountService;

//    查询所有账务账号信息
    @RequestMapping("/selectAllAccount")
   public String selectAllAccount(HttpServletRequest request){
       System.out.println("执行账务账号的查询功能");
       List<Account> accountList=accountService.selectAllAccount();
       request.setAttribute("accountList",accountList);
       System.out.println(accountList);
       return "account/account_list";
   }

//   更新开通状态
    @RequestMapping("/updateStatus")
    public String updateStatus(Integer account_id,Date pause_date,Integer status){
        System.out.println(account_id);
        //获取当?时间
        pause_date=new Date();
        System.out.println(pause_date);
        Date close_date=new Date();
        System.out.println(status);
//            改变开通状态
        if (status==1){
            status=0;
            int result=  accountService.updateStatus(account_id,status,pause_date);
            System.out.println(result+"更新状态为关闭,存储暂停时间");
            return "account/account_list";
        }else {
//            不是第一次开通，删除close——date，更新状态为开通，删除暂停时间，记载删除时间
            status=1;
            int result=accountService.updateStatus1(account_id,status,close_date);
            System.out.println(result+"不是第一次开通,更新状态,记录删除时间");
            return "account/account_list";
        }

    }

//    添加数据
    @RequestMapping("/insertAccount")
    public String insertAccount(Account account,String referrerid){
        if(referrerid!=null){
            System.out.println(referrerid);
            Integer recommenderId=  accountService.selectIdByidcard(referrerid);
            System.out.println(recommenderId);
            account.setRecommenderId(recommenderId);
            account.setStatus("1");

            Date date=new Date();
            account.setCreateDate(date);

            System.out.println(account);
            int result=accountService.insertAccount(account);
            return "account/account_list";
        }else {
            account.setStatus("1");

            account.setCreateDate(new Date());
            System.out.println(account);
            int result=accountService.insertAccount(account);
            return "account/account_list";
        }
//

    }

//    删除账务账号信息
    @RequestMapping("/deleteAccount")
    public String deleteAccount(Integer account_id){
        Date close_date=new Date();
        System.out.println(account_id);
        System.out.println(close_date);
        int result=accountService.updateAccount(account_id,close_date);
        System.out.println(result+"删除成功");
        return "account/account_list";
    }


//    详细信息回显
    @RequestMapping("/selectAccountById")
    public String selectAccountById(Integer account_id,HttpServletRequest request){
        System.out.println(account_id);
        Account account=accountService.selectAccountById(account_id);
        request.setAttribute("account",account);
        Integer recommender=account.getRecommenderId();
        System.out.println(recommender+"查出推荐人id");
        if (recommender!=null){
            Account account1=accountService.selectidcardById(recommender);
            System.out.println(account1.getIdcardNo()+"查出推荐人省份证");
            request.setAttribute("account1",account1);
            System.out.println(account);
        }

        return "account/account_detail";
    }



//    进行回显和修改
    @RequestMapping("/updateAccountInfo")
    public String updateAccountInfo(Integer account_id,HttpServletRequest request){
//        信息的回显
        System.out.println(account_id);
        Account account=accountService.selectAccountById(account_id);
        request.setAttribute("account",account);
        Integer recommender=account.getRecommenderId();
        System.out.println(recommender+"查出推荐人id");
        if (recommender!=null){
            Account account1=accountService.selectidcardById(recommender);
            System.out.println(account1.getIdcardNo()+"查出推荐人省份证");
            request.setAttribute("account1",account1);
            System.out.println(account);
        }
        return "account/account_modi";
    }

   @RequestMapping("/save")
    public String save(Account account,String birth) throws ParseException {
       System.out.println(birth);
       Date birthdate = new SimpleDateFormat("yyyy年MM月dd日").parse(birth);
       account.setBirthdate(birthdate);
       System.out.println(account);
//      插入数据
       account.setStatus("1");


       account.setCreateDate(new Date());

       System.out.println(account);
       int result=accountService.updateAccountInfo(account);
       System.out.println(result+"修改成功");
        return "account/account_list";
    }


//    高级查询
    @RequestMapping("/selectAllAccountByCondition")
    public String selectAllAccountByCondition(Account account,HttpServletRequest request){
        System.out.println(account);
        List<Account> accountList=accountService.selectAllAccountByCondition(account);
        request.setAttribute("accountList",accountList);
        System.out.println(accountList);
        return "account/account_list";
    }

}